#!/bin/bash

set -x

# default to update source then build

TOP_DIR=$(pwd)
SRC_DIR=$TOP_DIR/lib_source
BD_DIR=$TOP_DIR/lib_build
BIN_DIR=$TOP_DIR/lib_bin

VER_OPT=1.2.8

export PATH=$TOP_DIR/cross/bin:$PATH

cmd=all64
if [ -n "$1" ] ; then
	cmd=$1
fi

if [ "$cmd" == "all64" ] || [ "$cmd" == "all32" ] || [ "$cmd" == "update" ] ; then
	mkdir -p $SRC_DIR || exit 0
	cd $SRC_DIR
	if [ ! -f zlib-$VER_OPT.tar.gz ] ; then
		wget http://sourceforge.net/projects/libpng/files/zlib/1.2.8/zlib-1.2.8.tar.gz/download
	fi
	cd ..
fi

if [ "$cmd" == "all64" ] || [ "$cmd" == "b64" ] || [ "$cmd" == "all32" ] || [ "$cmd" == "b32" ] ; then
	rm -rf $BIN_DIR/zlib
	mkdir -p $BD_DIR
	cd $BD_DIR
	rm -rf bd_zlib
	tar xzf $SRC_DIR/zlib-$VER_OPT.tar.gz
	mv zlib-$VER_OPT bd_zlib
	cd bd_zlib
	export BINARY_PATH=$BIN_DIR/zlib/bin
	export INCLUDE_PATH=$BIN_DIR/zlib/include
	export LIBRARY_PATH=$BIN_DIR/zlib/lib
	if [ "$cmd" == "all32" ] || [ "$cmd" == "b32" ] ; then
		cp contrib/asm686/match.S ./match.S
		make install -f win32/Makefile.gcc  LOC="-DASMV" OBJA=match.o PREFIX=i686-w64-mingw32- SHARED_MODE=1
	else
#		cp contrib/amd64/amd64-match.S ./match.S
#		make install -f win32/Makefile.gcc  LOC="-DASMV -DNO_UNDERLINE" OBJA=match.o PREFIX=x86_64-w64-mingw32- SHARED_MODE=1
		make install -f win32/Makefile.gcc  PREFIX=x86_64-w64-mingw32- SHARED_MODE=1
#		cp contrib/gcc_gvmat64/gvmat64.S ./match.S
#		make -f win32/Makefile.gcc  LOC="-DASMV -DNO_UNDERLINE" OBJA=match.o PREFIX=x86_64-w64-mingw32- SHARED_MODE=1 minigzip.exe
#		make install -f win32/Makefile.gcc  LOC="-DASMV -DNO_UNDERLINE" OBJA=match.o PREFIX=x86_64-w64-mingw32- SHARED_MODE=1
	fi
		

#	--enable-static --disable-shared
#	--enable-shared --disable-static

	cd ..
	cd ..
fi

